package com.treasure.hunt.mine

import android.app.Activity
import android.content.Intent
import android.widget.ImageView
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ImageUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.base.BaseAbstractActivity
import com.module.lemlin.base.loadRoundImage
import com.module.lemlin.custom.SpacingItemDecoration
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.treasure.hunt.R
import com.treasure.hunt.app.AppConstance
import com.treasure.hunt.entity.Merge
import com.treasure.hunt.mode.GoodsViewMode
import kotlinx.android.synthetic.main.activity_mine_merge.*


class MineMergeActivity : ViewModeActivity<GoodsViewMode>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MineMergeActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val recordsAdapter =
        object : BaseQuickAdapter<Merge, BaseViewHolder>(R.layout.item_mine_merge) {
            override fun convert(holder: BaseViewHolder, item: Merge) {
                val isHttps: Boolean = item.goods_image.substring(0, 5).contains("http") ||
                        item.goods_image.substring(0, 5).contains("https")
                holder.setText(R.id.tvMergeGoodsTitle, item.goods_title)
                    .setText(R.id.tvMergeGoodsTime, item.createtime_text)
                    .setText(R.id.tvMergeGoodsStatus, item.status_text)
                    .getView<ImageView>(R.id.ivMergeGoodsImage)
                    .loadRoundImage(
                        "${if (isHttps) "" else AppConstance.URL_IMAGE}${item.goods_image}",
                        radius = 5
                    )
            }
        }

    override val layoutResId: Int
        get() = R.layout.activity_mine_merge

    override fun viewModel(): GoodsViewMode {
        return createViewModel()
    }

    override fun stateBar(): StatusBar {
        return StatusBar(
            isLightMode = false,
            bottomColor = R.color.FF_020027,
            view = flMineMergeBar
        )
    }

    override fun initView() {
        ivMineMergeBack.setOnClickListener {
            finish()
        }
        rvMainMergeRecordBody.apply {
            val decoration = SpacingItemDecoration(
                vGap = ConvertUtils.dp2px(15f),
                hGap = ConvertUtils.dp2px(15f)
            )
            addItemDecoration(decoration)
        }.adapter = recordsAdapter.apply {
            animationEnable = true
            isAnimationFirstOnly = false
            setAnimationWithDefault(BaseQuickAdapter.AnimationType.AlphaIn)
        }
    }

    override fun initData() {
        viewModel.indexGetOrderLog()
    }

    override fun initViewMode() {
        viewModel.mergeLiveData.observe(this, Observer { response ->
            if (response.status == Status.FAILURE)
                toast(response.throwable?.msg ?: "未知错误")
            if (response.status != Status.SUCCESS)
                return@Observer
            response.httpData?.data?.let {
                recordsAdapter.setNewInstance(it)
            }
        })
    }
}